package cn.hd.controller;

import cn.hd.entity.*;
import cn.hd.service.AddressService;
import cn.hd.service.BillService;
import cn.hd.service.ProductdetailService;
import cn.hd.service.ReturnService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
public class BillController {
    @Autowired
    private BillService billService;
    @Autowired
    private AddressService addressService;
    @Autowired
    private ReturnService returnService;
    @Autowired
    private ProductdetailService productdetailService;

    @RequestMapping("/selectAllBill")
    public Object selectAllBill(@RequestParam(defaultValue = "1") Integer pagenum, Integer pagesize, Integer isZf, Integer uid) {
        Map map = new HashMap();
        map.put("isZf", isZf);
        map.put("uid", uid);
        PageInfo<Bill> page = billService.selectAllBill(pagenum, pagesize, map);
        if (page.getList().size() != 0) {
            return new Result("200", "查询成功", page);
        }
        return new Result("400", "查询失败", null);
    }

    @RequestMapping("/addBill")
    public Object addBill(Bill bill) {
        int i = billService.addBill(bill);
        if (i != 0) {
            return new Result("200", "添加成功！", null);
        }
        return new Result("400", "添加失败！", null);
    }

    @RequestMapping("/deleteBill")
    public Object deleteBill(Integer billid) {
        int i = billService.deleteBill(billid);
        if (i != 0) {
            return new Result("200", "删除成功！", null);
        }
        return new Result("400", "删除失败！", null);
    }

    @RequestMapping("/selectBillByBid")
    public Object selectBillByBid(Integer billid) {
        Bill bill = billService.selectBillByBid(billid);
        if (bill != null) {
            return new Result("200", "查询成功！", bill);
        }
        return new Result("400", "查无此订单", null);
    }

    @RequestMapping("/addBillAddress")
    public Object addBillAddress(Integer bid, Integer aid) {
        int i = billService.addBillAddress(bid, aid);
        if (i != 0) {
            Address address = addressService.selectByAid(aid);
            int j = billService.wsbill(address.getUname(), bid);
            if (j != 0) {
                return new Result("200", "添加成功！", null);
            }
        }
        return new Result("400", "添加失败！", null);
    }

    @RequestMapping("/addDingDan")
    public Object addDingDan(Long returnid, Long dzid, Long paynum, Integer uid) {
        Returnn returnn = returnService.selectByIdReturn(returnid);
        Productdetail productdetail = productdetailService.selectByPid(returnn.getPid());
        Address address = addressService.selectByAid(dzid);
        Bill bill = new Bill();
        bill.setFreight(returnn.getFee());
        bill.setBillname(address.getUname());
        bill.setProductname(productdetail.getPname());
        bill.setPrice(returnn.getPay());
        bill.setNum(paynum);
        int i = billService.addBill(bill);
        if (i != 0) {
            List<Bill> bills = billService.selectAllDD();
            for (int j = 0; j < bills.size(); i++) {
                billService.addBillAddress(bills.get(0).getBillid(), dzid);
                billService.addBillUser(bills.get(0).getBillid(), uid);
                return new Result("200", "添加成功！", null);
            }
        }
        return new Result("400", "添加失败！", null);

    }

    @RequestMapping("/selectDDID")
    public Object selectDDID() {
        int i = billService.selectDDId();
        return new Result("200", "查询成功", i);
    }

    @RequestMapping("/updateDDZTWZFByid")
    public Object updateDDZTWZFByid(Integer bid) {
        int i = billService.updateDDZTWZFByid(bid);
        if (i != 0) {
            return new Result("200", "修改成功", null);
        } else {
            return new Result("400", "修改失败", null);
        }
    }
}
